package com.ruoyi.smart.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.smart.config.CouchDBService;
import com.ruoyi.smart.domain.SmartProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.smart.mapper.SmartProductTestWaveformMapper;
import com.ruoyi.smart.domain.SmartProductTestWaveform;
import com.ruoyi.smart.service.ISmartProductTestWaveformService;

/**
 * 产品波形数据Service业务层处理
 * 
 * @author minwie
 * @date 2023-03-24
 */
@Service
public class SmartProductTestWaveformServiceImpl implements ISmartProductTestWaveformService 
{
    @Autowired
    private SmartProductTestWaveformMapper smartProductTestWaveformMapper;
    @Autowired
    private CouchDBService couchDBService;
    @Autowired
    private SmartProductServiceImpl smartProductService;

    /**
     * 查询产品波形数据
     * 
     * @param id 产品波形数据主键
     * @return 产品波形数据
     */
    @Override
    public SmartProductTestWaveform selectSmartProductTestWaveformById(Long id)
    {
        SmartProductTestWaveform smartProductTestWaveform=smartProductTestWaveformMapper.selectSmartProductTestWaveformById(id);
        smartProductTestWaveform.setData(couchDBService.getString(smartProductTestWaveform.getData()));
        return smartProductTestWaveform;
    }

    /**
     * 查询产品波形数据列表
     * 
     * @param smartProductTestWaveform 产品波形数据
     * @return 产品波形数据
     */
    @Override
    public List<SmartProductTestWaveform> selectSmartProductTestWaveformList(SmartProductTestWaveform smartProductTestWaveform)
    {
        List<SmartProductTestWaveform> list=smartProductTestWaveformMapper.selectSmartProductTestWaveformList(smartProductTestWaveform);
        for(SmartProductTestWaveform item : list){
            item.setData(couchDBService.getString(item.getData()));
        }
        return list;
    }
    @Override
    public Map<String,List<SmartProductTestWaveform>> getSmartProductTestWaveformMap(Long id){
        //初始化数据
        Map<String,List<SmartProductTestWaveform>> map=new HashMap<>();
        map.put("写ID",new ArrayList<>());
        map.put("功能测试",new ArrayList<>());
        map.put("高压测试",new ArrayList<>());
        map.put("高温测试",new ArrayList<>());
        map.put("检测功能",new ArrayList<>());
        //根据产品id查询到产品
        SmartProduct smartProduct=smartProductService.selectSmartProductById(id);
        if(smartProduct!=null){
            SmartProductTestWaveform smartProductTestWaveform=new SmartProductTestWaveform();
            smartProductTestWaveform.setProductCode(smartProduct.getCode());
            List<SmartProductTestWaveform> list=selectSmartProductTestWaveformList(smartProductTestWaveform);
            for(SmartProductTestWaveform item : list){
                if(map.containsKey(item.getNotes())){
                    map.get(item.getNotes()).add(item);
                }else {
                    map.put(item.getNotes(),new ArrayList<>());
                    map.get(item.getNotes()).add(item);
                }
            }
        }
        return map;
    }

    /**
     * 新增产品波形数据
     * 
     * @param smartProductTestWaveform 产品波形数据
     * @return 结果
     */
    @Override
    public int insertSmartProductTestWaveform(SmartProductTestWaveform smartProductTestWaveform)
    {

        String key= couchDBService.insert(smartProductTestWaveform.getData());
        smartProductTestWaveform.setData(key);
        return smartProductTestWaveformMapper.insertSmartProductTestWaveform(smartProductTestWaveform);
    }

    /**
     * 修改产品波形数据
     * 
     * @param smartProductTestWaveform 产品波形数据
     * @return 结果
     */
    @Override
    public int updateSmartProductTestWaveform(SmartProductTestWaveform smartProductTestWaveform)
    {
        SmartProductTestWaveform tmp=smartProductTestWaveformMapper.selectSmartProductTestWaveformById(smartProductTestWaveform.getId());
        couchDBService.update(tmp.getData(),smartProductTestWaveform.getData());
        smartProductTestWaveform.setData(tmp.getData());
        return smartProductTestWaveformMapper.updateSmartProductTestWaveform(smartProductTestWaveform);
    }

    /**
     * 批量删除产品波形数据
     * 
     * @param ids 需要删除的产品波形数据主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestWaveformByIds(Long[] ids)
    {
        List<SmartProductTestWaveform> list=smartProductTestWaveformMapper.selectSmartProductTestWaveformByIds(ids);
        for(SmartProductTestWaveform item : list){
            couchDBService.delete(item.getData());
        }
        return smartProductTestWaveformMapper.deleteSmartProductTestWaveformByIds(ids);
    }

    /**
     * 删除产品波形数据信息
     * 
     * @param id 产品波形数据主键
     * @return 结果
     */
    @Override
    public int deleteSmartProductTestWaveformById(Long id)
    {
        SmartProductTestWaveform smartProductTestWaveform=smartProductTestWaveformMapper.selectSmartProductTestWaveformById(id);
        couchDBService.delete(smartProductTestWaveform.getData());
        return smartProductTestWaveformMapper.deleteSmartProductTestWaveformById(id);
    }
}
